﻿using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sh3h.chuansha.job.Jobs
{
    public class SynHotlineFileJob : BackgroundService
    {
        private readonly IConfiguration configuration;

        private readonly ISqlSugarClient db;

        public SynHotlineFileJob(IConfiguration _configuration, ISqlSugarClient _db)
        {
            configuration = _configuration;
            db = _db;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    Serilog.Log.Information("SynHotlineFileJob running at: {time}", DateTimeOffset.Now);
                    Serilog.Log.Information("SynHotlineFileJob begin");
                    await synVoid();
                    await Task.Delay(1000 * 60 * 60, stoppingToken);
                }
            }
            catch (OperationCanceledException)
            {
                // When the stopping token is canceled, for example, a call made from services.msc,
                // we shouldn't exit with a non-zero exit code. In other words, this is expected...
            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex, $" SynHotlineFileJob {ex.Message}");

                // Terminates this process and returns an exit code to the operating system.
                // This is required to avoid the 'BackgroundServiceExceptionBehavior', which
                // performs one of two scenarios:
                // 1. When set to "Ignore": will do nothing at all, errors cause zombie services.
                // 2. When set to "StopHost": will cleanly stop the host, and log errors.
                //
                // In order for the Windows Service Management system to leverage configured
                // recovery options, we need to terminate the process with a non-zero exit code.
                Environment.Exit(1);
            }
        }

        private async Task synVoid()
        {
            try
            {
                YWTB_WS.YWTB_WSSoapClient client = new YWTB_WS.YWTB_WSSoapClient(YWTB_WS.YWTB_WSSoapClient.EndpointConfiguration.YWTB_WSSoap);
                string sqlNoPushHotLineFile = @"SELECT S_KEFUBH,I_KID,S_GONGDANBH 
FROM BW_GONGDAN WHERE s_kefubh IS NOT NULL AND D_EXECUTIVETIME >to_date('2025-01-01','yyyy-mm-dd')
 ";

                var dt = db.Ado.GetDataTable(sqlNoPushHotLineFile);

                foreach (DataRow item in dt.Rows)
                {
                    string serialNumber = "";
                    try
                    {
                        serialNumber = item["S_KEFUBH"].ToString();
                        string gongDanBh = item["S_GONGDANBH"].ToString();
                        String kid = item["I_KID"].ToString();

                        string sqlFile = @"SELECT S_WENJIANMC,S_WENJIANLJ
FROM  sys_upload 
WHERE S_LEIXING = 'RXGD' 
AND S_BIANHAO = '" + kid + "'";
                        var dt1 = db.Ado.GetDataTable(sqlFile);

                        for (global::System.Int32 i = 0; i < dt1.Rows.Count; i++)
                        {
                            var dr = dt1.Rows[i];
                            string WJLJ = dr["S_WENJIANLJ"].ToString();
                            //base64WJ = DocumentToBase64Str(WJLJ);
                            string WJFILE = dr["S_WENJIANMC"].ToString();
                            //}
                            string JGMessage = "";
                            //WJLJ = WJLJ.Replace("/" + WJFILE, String.Empty);
                            int q = WJLJ.LastIndexOf('/');
                            int t = WJLJ.LastIndexOf('\\');
                            string SCLJ = "";
                            string SCMC = "";
                            if (q > t)
                            {
                                SCMC = WJLJ.Substring(q + 1, WJLJ.Length - q - 1);
                                SCLJ = WJLJ.Substring(0, q);
                            }
                            else
                            {
                                SCMC = WJLJ.Substring(t + 1, WJLJ.Length - t - 1);
                                SCLJ = WJLJ.Substring(0, t);
                            }


                            var res = client.CSKHDSCWJN(serialNumber, SCLJ, SCMC);

                            Log.Information(serialNumber + " res :" + res.ToString());

                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Information(serialNumber + " error :" + ex.Message.ToString());

                    }
                }
            }
            catch (Exception ex)
            {
                Log.Information("SynHotlineFileJob error :" + ex.Message.ToString());
            }


        }
    }
}

